package com.example.edianzu.somur.utils.view;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.edianzu.somur.R;
import com.example.edianzu.somur.utils.content.L;

/**
 * author：风不会停息 on 2017/7/22 18:53
 * mailbox：yh131412hys@163.com
 * project：Somur
 * tel：13295355900
 * describe：
 */

public abstract class ContentFragment extends Fragment {


    private TextView titleView;
    private Toolbar toolbar;
    private LinearLayout contentView;
    private CoordinatorLayout rootView;
    public BaseActivity activity;
    private NavigationIconOnClickListener navigationIconOnClickListener;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = null;
        if (view == null) {
            view = inflater.inflate(R.layout.content_view_base, container, false);
        }

        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        initToolbar();
        loadCustomerLayout();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        navigationIconOnClickListener = (NavigationIconOnClickListener) context;
        activity = context instanceof BaseActivity ? (BaseActivity) context : null;

    }

    private void initView(View view) {
        titleView = (TextView) view.findViewById(R.id.title);
        toolbar = (Toolbar) view.findViewById(R.id.toolbar);
        contentView = (LinearLayout) view.findViewById(R.id.content_view);
        rootView = (CoordinatorLayout) view.findViewById(R.id.root_view);

    }

    private void initToolbar() {
        toolbar.setTitle("");
        ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
        ((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        ((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);
        toolbar.setNavigationIcon(R.drawable.ic_arrow);
        toolbar.setNavigationOnClickListener(onClickListener);

    }


    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            L.e("点击了返回箭头");
            if (navigationIconOnClickListener != null) {
                navigationIconOnClickListener.goBackListener();
            }
        }
    };

    public interface NavigationIconOnClickListener {
        void goBackListener();
    }

    public void setNavigationIconOnClickListener(NavigationIconOnClickListener navigationIconOnClickListener) {
        this.navigationIconOnClickListener = navigationIconOnClickListener;
    }

    /**
     * 加载自定义视图
     *
     * @param
     */
    public void loadCustomerLayout() {

        View childView = View.inflate(getContext(), customerLayoutId(), null);
        contentView.addView(childView, new WindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT));


    }

    public void setToolbarTitle(String title) {
        titleView.setVisibility(View.VISIBLE);
        titleView.setText(title);
    }

    protected abstract int customerLayoutId();
}
